package com.training.killerapp;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.tools.cli.AbstractCli;

import com.training.killerapp.model.User;

/**
 * The main entry-point for the Killer App command line interface (CLI).
 * 
 * @author Eric Redmond
 * @since 1.0
 */
public class Main extends AbstractCli
{
    /**
     * The main entry point for the Killer App.
     * 
     * @param args command-line input arguments.
     * @throws ParseException if the initial command-line input can not be parsed.
     */
    public static void main( String[] args )
        throws Exception
    {
        Main main = new Main();
        main.execute( args );
    }

    /**
     * 
     */
    public Options buildCliOptions( Options options )
    {
        options.addOption( new Option( "p", "Prints out all of the user emails" ) );
        options.addOption( new Option( "c", "Begins a create user prompts loop" ) );
        options.addOption( new Option( "d", true, "Delete the User with the given email" ) );

        return options;
    }

    /**
     * 
     */
    public void invokePlexusComponent( CommandLine commandline, PlexusContainer plexuscontainer ) throws Exception
    {
        KillerApp killerApp = (KillerApp)plexuscontainer.lookup( "com.training.killerapp.KillerApp" );

        if( commandline.hasOption( "d" ) )
        {
            User user = new User();
            user.setEmail( commandline.getOptionValue( "d" ) );
            killerApp.removeUserAndPage( user );
        }

        if( commandline.hasOption( "c" ) )
        {
            while( true )
            {
                System.out.println( "Please enter a new user email:" );
                String email = readInput();
                System.out.println( "Please enter the new user name:" );
                String userName = readInput();
                System.out.println( "Please enter the new page name:" );
                String pageName = readInput();

                try
                {
                    User newUser = killerApp.createAccount( email, email, userName );

                    killerApp.managePage( newUser, pageName, "NO CONTENTS" );
                }
                catch( SecurityException e )
                {
                    System.out.println( "User already exists with given email" );
                }

                System.out.println( "Would you like to add another user? y/n" );

                if( !"y".equals( readInput() ) )
                {
                    break;
                }
            }
        }

        if( commandline.hasOption( "p" ) )
        {
            System.out.println( "Printing User email List..." );

            for( Iterator users = killerApp.getUserIterator(); users.hasNext(); )
            {
                User user = (User)users.next();

                String email = user.getEmail();
                if( email != null )
                {
                    System.out.println( email );
                }
            }
        }

    }

    /**
     * Reads the users command line inputs. Halts when a newline character (\n)
     * is encountered.
     * @return a trimmed String value entered before the newline.
     */
    private String readInput()
    {
        InputStreamReader inputReader = new InputStreamReader( System.in );
        String input = "";
        try
        {
            char inputChar = 0;
            while( ( inputChar = (char)inputReader.read() ) != '\n' )
            {
                input += inputChar;
            }
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
        return input.trim();
    }
}
